﻿using DearlerPlatform.Domain;
using DelaerPlatform.Core.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DelaerPlatform.Core.Repository
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class //where TEntity:BaseEntity
    {
        private readonly DelaerPlatformDbContext context;

        public Repository(DelaerPlatformDbContext context)
        {
            this.context = context;
        }

        public List<TEntity> GetList()
        {
            return context.Set<TEntity>().ToList();
        }

        public List<TEntity> GetList(Func<TEntity, bool> predicate)
        {
            return context.Set<TEntity>().Where(predicate).AsQueryable().ToList();

        }

        public async Task<List<TEntity>> GetListAsync()
        {
            return await context.Set<TEntity>().ToListAsync();
        }

        public async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await context.Set<TEntity>().Where(predicate).AsQueryable().ToListAsync();
            //return await context.Set<TEntity>().WhereAsync(predicate).ToListAsync();

        }


        public TEntity Get(Func<TEntity, bool> predicate)
        {
            return context.Set<TEntity>().FirstOrDefault(predicate);

        }

        public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await context.Set<TEntity>().FirstOrDefaultAsync(predicate);
        }

        public TEntity Insert(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = dbSet.Add(entity).Entity;
            context.SaveChanges();
            return res;
        }
        public async Task<TEntity> InsertAsync(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = (await dbSet.AddAsync(entity)).Entity;
            await context.SaveChangesAsync();
            return res;
        }
        public TEntity Delete(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = dbSet.Remove(entity).Entity;
            context.SaveChanges();
            return res;
        }
        public async Task<TEntity> DeleteAsync(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = dbSet.Remove(entity).Entity;
            await context.SaveChangesAsync();
            return res;
        }
        public TEntity Update(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = dbSet.Update(entity).Entity;
            context.SaveChanges();
            return res;
        }
        public async Task<TEntity> UpdateAsync(TEntity entity)
        {
            var dbSet = context.Set<TEntity>();
            var res = dbSet.Update(entity).Entity;
            await context.SaveChangesAsync();
            return res;
        }

    }
}
